package com.example.tests;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.AssertJUnit;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import com.example.fw.ContactHelper;
import com.example.utils.SortedListOf;

import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;

public class ContactCreationTests extends TestBase {
	
	@DataProvider
	public Iterator<Object[]> contactsFromFile () throws IOException {
		return wrapContactsDataForDataProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}
	
	@Test(dataProvider = "contactsFromFile")
	public void testContactCreationWithValidData(ContactData contact) throws Exception {
		//save old state
		//ContactHelper contactHelper = app.getContactHelper();
		SortedListOf<ContactData> oldList = app.getModel().getContacts();
		
		
		//actions
		app.getContactHelper().createContact(contact);
		
		//save new state
		SortedListOf<ContactData> newList = app.getModel().getContacts();
		
		//compare states
		assertThat(newList, equalTo(oldList.withAdded(contact)));
		
		//compare model to implementation
		if(wantToCheck()) {
			if("yes".equals(app.getProperty("check.db"))) {
				assertThat(app.getModel().getContacts(), equalTo(app.getHibernateHelper().listContacts()));
		}
			if("yes".equals(app.getProperty("check.ui"))) {
				assertThat(app.getModel().getContacts(), equalTo(app.getContactHelper().getUiContacts()));
			}
		}
	}
	
}
